//
//  screenOneView.m
//  何五路
//
//  Created by _ADY on 15/6/19.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "screenOneView.h"

@implementation screenOneView
@synthesize sxListArray,delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        vFrame = frame;
        
        [self allButton];
        
        myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, frame.size.width, frame.size.height - 64) style:UITableViewStyleGrouped];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.sectionHeaderHeight = 0.0;
        myTableView.sectionFooterHeight = 0.0;
        myTableView.backgroundColor = bgColor;
        [self addSubview:myTableView];
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
        [myTableView setTableFooterView:v];
        current = 0;
        
        [self performSelector:@selector(after) withObject:nil afterDelay:.2];
       

    }
    return self;
}

-(void)after
{
     NSLog(@"sxListArray %@",sxListArray);
    
    set = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *mset = [[NSMutableArray alloc] initWithCapacity:10];
    for ( NSDictionary* d in sxListArray)
    {
        NSString* FlowType = [NSString stringWithFormat:@"%@",[d objectForKey:@"SpecificationAttributeId"]];
        BOOL Yess = NO;
        for (NSDictionary* menu in set)
        {
            NSString* menuType = [NSString stringWithFormat:@"%@",[menu objectForKey:@"SpecificationAttributeId"]];
            
            if ([menuType isEqualToString:FlowType])
            {
                Yess = YES;
                break;
            }
        }
        if (!Yess) {
            if (![screenItem unarchiveWithKey:@"screenDicYes"])
            {
                NSMutableDictionary *dic  = [NSMutableDictionary dictionaryWithCapacity:1];
                [dic setObject:@"0" forKey:@"OptionId"];
                [dic setObject:@"全部" forKey:@"OptionName"];
                [dic setObject:[NSString stringWithFormat:@"%@",[d objectForKey:@"SpecificationAttributeId"]] forKey:@"AttributeId"];
                
                [mset addObject:dic];
            }

            [set addObject:d];
        }
        
    }
    if (![screenItem unarchiveWithKey:@"screenDicYes"])
        [mset archiveWithKey:@"screenDicYes"];
    
    if ([screenItem unarchiveWithKey:@"screenDicYes"])
    {
        [[screenItem unarchiveWithKey:@"screenDicYes"] archiveWithKey:@"screenDic"];
    }
    
    
    for (int i = 0; i < 3; i ++)
    {
        if (![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"switchYesBool%d",i]])
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"switchBool%d",i]];
        }
        else
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"switchBool%d",i]];
    }
    
    [myTableView reloadData];
}

-(void)allButton
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, vFrame.size.width, .5)];
    lineView.backgroundColor = [UIColor colorWithRed:191/255.0 green:191/255.0 blue:191/255.0 alpha:1];
    [self addSubview:lineView];
    
    
    UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [inButton setFrame:CGRectMake(0,24, 50, 40)];
    [inButton setTitle:@"取消" forState:UIControlStateNormal];
    [inButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [inButton addTarget:self action:@selector(qxBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:inButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,24, vFrame.size.width-100, 40)];
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = 1;
    titleLabel.text = @"筛选";
    [self addSubview:titleLabel];
    
    UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [yesButton setFrame:CGRectMake(vFrame.size.width-50,24, 50, 40)];
    [yesButton setTitle:@"确定" forState:UIControlStateNormal];
    [yesButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    yesButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [yesButton addTarget:self action:@selector(yesBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:yesButton];
    
    
}

-(void)qxBction
{
    [self.delegate JKSshow];
}

-(void)yesBction
{

    if ([screenItem unarchiveWithKey:@"screenDic"])
    {
        [[screenItem unarchiveWithKey:@"screenDic"] archiveWithKey:@"screenDicYes"];
    }
    
    for (int i = 0; i < 3; i ++)
    {
        if (![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"switchBool%d",i]])
        {
            [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"switchYesBool%d",i]];
        }
        else
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"switchYesBool%d",i]];
    }
   [self.delegate JKSshow];
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.001;
    }
    return 10;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
        return  1;
    else if (section == 1)
        return  3;
    else if (section == 3)
        return 1;
    return set.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"screenOneCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.section == 0)
    {
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];
    }
    else if (indexPath.section == 1)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        return [self mCell:cell bCTableView:aTableView cellForRowAtIndexPath:indexPath];
    }
    else if (indexPath.section == 2)
        return [self mCell:cell cCTableView:aTableView cellForRowAtIndexPath:indexPath];
    else if (indexPath.section == 3)
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        UIButton *inButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [inButton setFrame:CGRectMake(0,0, 150, 40)];
        inButton.center = CGPointMake(vFrame.size.width/2, 50/2);
        [inButton setTitle:@"清除选项" forState:UIControlStateNormal];
        inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
        [inButton addTarget:self action:@selector(qcxxBction) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:inButton];
    }
    // Configure the cell...
    return cell;
}
- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.text = @"配送至";
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.text = [SendService address];
    cell.detailTextLabel.textAlignment = 2;
    cell.detailTextLabel.textColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];
    
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell bCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [NSArray arrayWithObjects:@"五路配送",@"仅看有货",@"货到付款", nil];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor blackColor];
    
    
    UISwitch *swi1=[[UISwitch alloc]init];
    swi1.frame=CGRectMake(vFrame.size.width-60, 7, 60, 40);
    swi1.onTintColor=[UIColor redColor];
    if (![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"switchBool%d",indexPath.row]])
    {
        [swi1 setOn:NO animated:YES];
    }
    else
        [swi1 setOn:YES animated:YES];
    swi1.tag = indexPath.row +10;
    [swi1 addTarget:self action:@selector(getValue:) forControlEvents:UIControlEventValueChanged];
    [cell.contentView addSubview:swi1];
    

    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell cCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.textLabel.text = [[set objectAtIndex:indexPath.row] objectForKey:@"SpecificationAttributeName"];
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor blackColor];

     cell.detailTextLabel.textColor = [UIColor grayColor];
    NSMutableArray *array = [screenItem unarchiveWithKey:@"screenDic"];
    if (array.count > indexPath.row)
        cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"OptionName"];
    else
        cell.detailTextLabel.text = @"全部";
    cell.detailTextLabel.textAlignment = 2;
    
    if (![cell.detailTextLabel.text isEqualToString:@"全部"]) {
        cell.detailTextLabel.textColor = [UIColor redColor];
    }
   

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    current=indexPath.row;
    if (indexPath.section == 2)
    {
        [self gotoTwoView:[[set objectAtIndex:indexPath.row] objectForKey:@"SpecificationAttributeId"]];
    }
    else if (indexPath.section == 0)
    {
        addressView *mView = (addressView*)[self viewWithTag:4];
        if (mView != nil)
        {
            [mView removeFromSuperview];
            mView = nil;
        }
        addressView *twoViw = [[addressView alloc] initWithFrame:CGRectMake(vFrame.size.width, 0, vFrame.size.width, vFrame.size.height)];
        twoViw.tag = 4;
        twoViw.delegate = self;
        [self addSubview:twoViw];
        [UIView animateWithDuration:.5 animations:^{
            twoViw.frame = CGRectMake(0, 0, vFrame.size.width, vFrame.size.height);
        }];
    }
}

-(void)closeAddressView
{
    addressView *mView = (addressView*)[self viewWithTag:4];
    if (mView != nil)
    {
        [UIView animateWithDuration:.5 animations:^{
            mView.frame = CGRectMake(vFrame.size.width, 0, vFrame.size.width, vFrame.size.height);
        }];
    }
    [myTableView reloadData];
}
-(void)getValue:(id)sender{
    UISwitch *swi2=(UISwitch *)sender;

    [[NSUserDefaults standardUserDefaults] setBool:swi2.isOn forKey:[NSString stringWithFormat:@"switchBool%d",swi2.tag-10]];

}

-(void)gotoTwoView:(NSString*)AttributeId
{
    screenTwoView *mView = (screenTwoView*)[self viewWithTag:3];
    if (mView != nil)
    {
        [mView removeFromSuperview];
        mView = nil;
    }
    screenTwoView *twoViw = [[screenTwoView alloc] initWithFrame:CGRectMake(vFrame.size.width, 0, vFrame.size.width, vFrame.size.height)];
    twoViw.tag = 3;
    twoViw.sxListArray = sxListArray;
    twoViw.AttributeId = AttributeId;
    twoViw.delegate = self;
    [self addSubview:twoViw];
    [UIView animateWithDuration:.5 animations:^{
        twoViw.frame = CGRectMake(0, 0, vFrame.size.width, vFrame.size.height);
    }];
}

-(void)closeTwoView
{
    screenTwoView *mView = (screenTwoView*)[self viewWithTag:3];
    if (mView != nil)
    {
        [UIView animateWithDuration:.5 animations:^{
            mView.frame = CGRectMake(vFrame.size.width, 0, vFrame.size.width, vFrame.size.height);
        }];
    }
    [myTableView reloadData];
}

#pragma mark 清除选项
-(void)qcxxBction
{
    [BDModel cleanAllArchiveFile];
    
    [self after];
    
    for (int i = 0; i < 3; i ++)
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"switchBool%d",i]];
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:[NSString stringWithFormat:@"switchYesBool%d",i]];

    }

    [myTableView reloadData];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
